﻿namespace EasyWeb
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using EasyWeb.Data;
    using EasyWeb.Design;

    [EWDisplayName(Name = "FieldDefinitionDateTime", ResourceType = typeof(EWFieldDefinitionResources))]
    public class EWFieldDefinitionDateTime : EWFieldDefinition
    {
        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowDefaultValue")]
        public override bool AllowDefaultValue
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowUnique")]
        public override bool AllowUnique
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_DefaultValue")]
        public override object DefaultValue
        {
            get { return this.StateBag.Get<DateTime?>("DefaultValue"); }
            set { this.StateBag.Set("DefaultValue", (DateTime?)value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionDateTime_Mode")]
        public EWDateTimeMode Mode
        {
            get { return this.StateBag.Get<EWDateTimeMode>("Mode", () => EWDateTimeMode.Date); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("Mode", value);
            }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionDateTime_IsNowDefaultValue")]
        public bool IsNowDefaultValue
        {
            get { return this.StateBag.Get<bool>("IsNowDefaultValue"); }
            set { this.StateBag.Set("IsNowDefaultValue", value); }
        }

        public override EWFieldDefinitionColumnCollection CreateFieldDefinitionColumns()
        {
            var field = new EWFieldDefinitionColumn()
            {
                Name = this.InternalName,
                IsNullable = this.AllowNull,
                IsUnique = this.AllowUnique && this.IsUnique,
            };

            switch (this.Mode)
            {
                case EWDateTimeMode.Date:
                    field.Type = DbType.Date;
                    break;
                case EWDateTimeMode.Time:
                    field.Type = DbType.Time;
                    break;
                case EWDateTimeMode.DateTime:
                    field.Type = DbType.DateTime;
                    break;
            }

            return new EWFieldDefinitionColumnCollection()
            {
                field
            };
        }

        public override void Deserialize(EWSerializationInfo info)
        {
            base.Deserialize(info);

            this.Mode = info.GetEnum<EWDateTimeMode>("Mode");
            this.IsNowDefaultValue = info.GetBoolean("IsNowDefaultValue");
            this.DefaultValue = info.GetNullableDateTime("DefaultValue");
        }

        public override void Serialize(EWSerializationInfo info)
        {
            base.Serialize(info);

            info.AddValue("Mode", this.Mode);
            info.AddValue("IsNowDefaultValue", this.IsNowDefaultValue);
            info.AddValue("DefaultValue", (DateTime?)this.DefaultValue);
        }
    }
}
